<template>
  <el-dialog
    :visible.sync="dialog"
    :loading="loading"
    :close-on-click-modal="false"
    :title="titleMap[dialogStatus]"
    width="75%"
    left
    @close="closeDlg"
  >
    <div class="app-container">
      <div style="padding-top:10px">
        <el-form ref="dataInfoRef" :model="dataInfo" :inline="true" :rules="rules" size="small" label-position="right" label-width="200px">
          <el-form-item label="驳回原因类型" prop="type">
            <el-select v-model="dataInfo.type" placeholder="选择搜索" clearable size="mini" style="width: 200px">
              <el-option label="预审驳回-文描服务商" value="0" />
              <el-option label="初审驳回-线上品类运营" value="1" />
              <el-option label="终审驳回-GBS团队" value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="驳回原因:" prop="reason">
            <el-input v-model="dataInfo.reason" type="textarea" style="width: 500px;" class="filter-item" :disabled="diasabledInput" />
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-row :gutter="20">
      <el-col :span="1" :offset="8"><div class="grid-content bg-purple"><el-button type="primary" size="small" @click="onSubmit(dataInfo)">提交</el-button></div></el-col>
      <el-col :span="1" :offset="4"><div class="grid-content bg-purple"><el-button type="primary" size="small" @click="closeDlg">取消</el-button></div></el-col>
    </el-row>
  </el-dialog>
</template>

<script>
// views/example/list
// import fileSaver from 'file-saver'
// import xlsx from 'xlsx'
// import initData from '@/utils/initData'
import initData from '@/utils/reason/initData'
import { Message } from 'element-ui'
import { insertInfo, updateInfo, checkLong } from '@/api/reason/data'

export default {
  mixins: [initData],
  props: {
    parent: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      dialog: false,
      diasabledInput: false,
      citysMap: [],
      checkedCitys: [],
      dataInfo: {
        id: '',
        type: '',
        reason: ''
      },
      loading: false,
      titleMap: {
        addMap: '新增驳回原因',
        editMap: '编辑驳回原因'
      },
      dialogStatus: '',
      rules: { // 表单验证规则
        reason: [
          { required: true, message: '请输入驳回原因', trigger: 'blur' },
          { validator: checkLong, length: 30, trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择驳回原因类型', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    closeDlg() {
      this.$refs.dataInfoRef.resetFields()
      this.dialog = false
    },
    // 提交触发
    onSubmit(dataInfo) {
      this.$refs.dataInfoRef.validate(valid => {
        if (valid) {
          this.loading = true
          this.$confirm('是否保存该信息?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            if (this.dataInfo.id) {
              updateInfo(this.dataInfo).then(res => {
                this.loading = false
                this.parent.toQuery()
                if (res.message !== 'success') {
                  Message({
                    message: res,
                    type: 'error',
                    duration: 3 * 1000
                  })
                  this.dialog = true
                } else {
                  Message({
                    message: '修改成功',
                    type: 'success',
                    duration: 3 * 1000
                  })
                  this.dialog = false
                }
              })
            } else {
              insertInfo(this.dataInfo).then(res => {
                this.loading = false
                this.parent.toQuery()
                if (res.message !== 'success') {
                  Message({
                    message: res,
                    type: 'error',
                    duration: 3 * 1000
                  })
                  this.dialog = true
                } else {
                  Message({
                    message: '新增驳回原因',
                    type: 'success',
                    duration: 3 * 1000
                  })
                  this.dialog = false
                }
              })
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style scoped>
</style>
